import Vue from 'vue';
import Router from 'vue-router';
import { checkLogin } from '@/api/common';
import NProgress from 'nprogress';
import './style/nprogress.css';
Vue.use(Router);

const router = new Router({
  mode: 'history',
  routes: [
    {
      path: '/',
      name: 'commonIndex',
      redirect: '/home',
      component: resolve => {
        require(['./commonPages/Index'], resolve);
      },
      children: [
        {
          path: '/home',
          name: 'home',
          component: resolve => {
            require(['./commonPages/Index/pages/Home'], resolve);
          }
        },
        {
          path: '/subject/:link',
          name: 'subject',
          component: resolve => {
            require(['./commonPages/Index/pages/Subject'], resolve);
          }
        }
      ]
    },
    {
      name: '404',
      path: '/404',
      component: () => import('@/commonPages/404')
    },
    {
      path: '*', // 此处需特别注意至于最底部
      redirect: '/404'
    },
    {
      path: '/signup',
      name: 'signup',
      component: resolve => {
        require(['./studentUserComponents/Signup'], resolve);
      }
    },
    {
      path: '/teacherSignup',
      name: '/teacherSignup',
      component: resolve => {
        require(['./teacherUserComponents/Signup'], resolve);
      }
    },
    {
      path: '/about',
      name: 'about',
      component: () =>
        import(/* webpackChunkName: "about" */ './commonPages/About'),
      children: [
        {
          path: '/about/recruit',
          name: 'recruit',
          component: () =>
            import(
              /* webpackChunkName: "about" */ './commonPages/About/pages/Recruit'
            )
        },
        {
          path: '/about/aboutus',
          name: 'aboutus',
          component: () =>
            import(
              /* webpackChunkName: "about_aboutus" */ './commonPages/About/pages/AboutUs'
            )
        },
        {
          path: '/about/ourteam',
          name: 'ourteam',
          component: () =>
            import(
              /* webpackChunkName: "about_ourteam" */ './commonPages/About/pages/Team'
            )
        },
        {
          path: '/about/job',
          name: 'job',
          component: () =>
            import(
              /* webpackChunkName: "about_job" */ './commonPages/About/pages/Job'
            )
        },
        {
          path: '/about/contact',
          name: 'contact',
          component: () =>
            import(
              /* webpackChunkName: "about_contact" */ './commonPages/About/pages/Contact'
            )
        },
        {
          path: '/about/friendly',
          name: 'friendly',
          component: () =>
            import(
              /* webpackChunkName: "about_friendly" */ './commonPages/About/pages/Friendly'
            )
        }
      ]
    },
    {
      path: '/login',
      name: 'login',
      component: () =>
        import(/* webpackChunkName: "loginPage" */ '@/commonPages/Login')
    },
    {
      path: '/crouse_info',
      name: 'crouse',
      component: () =>
        import(/* webpackChunkName: "Crouse_info" */ '@/commonPages/CrouseInfo')
    },
    {
      path: '/teacher_center',
      name: 'teacher_center',
      redirect: '/teacher_center/info',
      component: () =>
        import(
          /* webpackChunkName: "teacher_center" */ '@/teacherUserComponents/Center'
        ),
      children: [
        {
          path: '/teacher_center/info',
          name: 'teacher_center_info',
          component: () =>
            import(
              /* webpackChunkName: "teacher_center" */ '@/teacherUserComponents/Center/pages/Info'
            )
        },
        {
          path: '/teacher_center/release',
          name: 'teacher_center_release',
          component: () =>
            import(
              /* webpackChunkName: "teacher_center" */ '@/teacherUserComponents/Center/pages/Release'
            )
        },
        {
          path: '/teacher_center/management',
          name: 'teacher_center_management',
          component: () =>
            import(
              /* webpackChunkName: "teacher_center" */ '@/teacherUserComponents/Center/pages/Management.vue'
            )
        },
        {
          path: '/teacher_center/income',
          name: 'teacher_center_income',
          component: () =>
            import(
              /* webpackChunkName: "teacher_center" */ '@/teacherUserComponents/Center/pages/Income.vue'
            )
        },
        {
          path: '/teacher_center/questions',
          name: 'teacher_center_questions',
          component: () =>
            import(
              /* webpackChunkName: "teacher_center" */ '@/teacherUserComponents/Center/pages/Questions.vue'
            )
        },
        {
          path: '/teacher_center/message',
          name: 'teacher_center_message',
          component: () =>
            import(
              /* webpackChunkName: "teacher_center" */ '@/teacherUserComponents/Center/pages/Message.vue'
            )
        }
      ]
    },
    {
      path: '/student_center',
      name: 'student_center',
      redirect: '/student_center/info',
      component: () =>
        import(
          /* webpackChunkName: "teacher_center" */ '@/studentUserComponents/Center'
        ),
      children: [
        {
          path: '/student_center/info',
          name: 'student_center_info',
          component: () =>
            import(
              /* webpackChunkName: "teacher_center" */ '@/studentUserComponents/Center/pages/Info'
            )
        },
        {
          path: '/student_center/release',
          name: 'student_center_release',
          component: () =>
            import(
              /* webpackChunkName: "teacher_center" */ '@/studentUserComponents/Center/pages/Release'
            )
        },
        {
          path: '/student_center/management',
          name: 'student_center_management',
          component: () =>
            import(
              /* webpackChunkName: "teacher_center" */ '@/studentUserComponents/Center/pages/Management.vue'
            )
        },
        {
          path: '/student_center/cart',
          name: 'student_center_cart',
          component: () =>
            import(
              /* webpackChunkName: "teacher_center" */ '@/studentUserComponents/Center/pages/Cart.vue'
            )
        },
        {
          path: '/student_center/questions',
          name: 'student_center_questions',
          component: () =>
            import(
              /* webpackChunkName: "teacher_center" */ '@/studentUserComponents/Center/pages/Questions.vue'
            )
        },
        {
          path: '/student_center/message',
          name: 'student_center_message',
          component: () =>
            import(
              /* webpackChunkName: "teacher_center" */ '@/studentUserComponents/Center/pages/Message.vue'
            )
        }
      ]
    },
    {
      path: '/teacher_center_s',
      name: 'teacher_center_s',
      component: () =>
        import(
          /* webpackChunkName: "teacher_center" */ '@/teacherUserComponents/SmallScreenCenter'
        ),
      children: [
        {
          path: '/teacher_center_s/info',
          name: 'teacher_center_s_info',
          component: () =>
            import(
              /* webpackChunkName: "teacher_center" */ '@/teacherUserComponents/SmallScreenCenter/pages/Info'
            )
        },
        {
          path: '/teacher_center_s/release',
          name: 'teacher_center_s_release',
          component: () =>
            import(
              /* webpackChunkName: "teacher_center" */ '@/teacherUserComponents/SmallScreenCenter/pages/Release'
            )
        },
        {
          path: '/teacher_center_s/management',
          name: 'teacher_center_s_management',
          component: () =>
            import(
              /* webpackChunkName: "teacher_center" */ '@/teacherUserComponents/SmallScreenCenter/pages/Management.vue'
            )
        },
        {
          path: '/teacher_center_s/income',
          name: 'teacher_center_s_income',
          component: () =>
            import(
              /* webpackChunkName: "teacher_center" */ '@/teacherUserComponents/SmallScreenCenter/pages/Income.vue'
            )
        },
        {
          path: '/teacher_center_s/questions',
          name: 'teacher_center_s_questions',
          component: () =>
            import(
              /* webpackChunkName: "teacher_center" */ '@/teacherUserComponents/SmallScreenCenter/pages/Questions.vue'
            )
        },
        {
          path: '/teacher_center_s/message',
          name: 'teacher_center_s_message',
          component: () =>
            import(
              /* webpackChunkName: "teacher_center" */ '@/teacherUserComponents/SmallScreenCenter/pages/Message.vue'
            )
        }
      ]
    },
    {
      path: '/student_center_s',
      name: 'student_center_s',
      component: () =>
        import(
          /* webpackChunkName: "teacher_center" */ '@/studentUserComponents/Center'
        ),
      children: [
        {
          path: '/student_center_s/info',
          name: 'student_center_s_info',
          component: () =>
            import(
              /* webpackChunkName: "teacher_center" */ '@/studentUserComponents/SmallScreenCenter/pages/Info'
            )
        },
        {
          path: '/student_center_s/release',
          name: 'student_center_s_release',
          component: () =>
            import(
              /* webpackChunkName: "teacher_center" */ '@/studentUserComponents/SmallScreenCenter/pages/Release'
            )
        },
        {
          path: '/student_center_s/management',
          name: 'student_center_s_management',
          component: () =>
            import(
              /* webpackChunkName: "teacher_center" */ '@/studentUserComponents/SmallScreenCenter/pages/Management.vue'
            )
        },
        {
          path: '/student_center_s/cart',
          name: 'student_center_s_cart',
          component: () =>
            import(
              /* webpackChunkName: "teacher_center" */ '@/studentUserComponents/SmallScreenCenter/pages/Cart.vue'
            )
        },
        {
          path: '/student_center_s/questions',
          name: 'student_center_s_questions',
          component: () =>
            import(
              /* webpackChunkName: "teacher_center" */ '@/studentUserComponents/SmallScreenCenter/pages/Questions.vue'
            )
        },
        {
          path: '/student_center_s/message',
          name: 'student_center_s_message',
          component: () =>
            import(
              /* webpackChunkName: "teacher_center" */ '@/studentUserComponents/SmallScreenCenter/pages/Message.vue'
            )
        }
      ]
    }
  ]
});
router.beforeEach((to, from, next) => {
  NProgress.start();
  const nextRoute = ['teacher_center', 'student_center', 'home'];
  //跳转至上述3个页面
  const fullPathRoot = to.path.split('/')[1];
  if (nextRoute.indexOf(fullPathRoot) >= 0) {
    checkLogin().then(res => {
      if (res.status === 200) {
        const data = res.data;
        if (data.status === 200) {
          const shenfen = data.result.msg.shenfen;
          const userImg = data.result.s_img;
          const userId = data.result.msg.t_id;
          if (shenfen === 'S') {
            router.app.$options.store.dispatch('student/changeInfo', {
              userImg,
              id: userId
            });
          }
          if (shenfen === 'T') {
            router.app.$options.store.dispatch('teacher/changeInfo', {
              userImg,
              id: userId
            });
          }
          router.app.$options.store.dispatch('login/loginStatus', {
            loginStatus: 1,
            identity: shenfen
          });
        } else {
          router.app.$options.store.dispatch('login/loginStatus', {
            loginStatus: 2,
            identity: ''
          });
        }
      } else {
        router.app.$options.store.dispatch('login/loginStatus', {
          loginStatus: 2,
          identity: ''
        });
      }
      const auth = router.app.$options.store.state.login.loginStatus;
      const identity = router.app.$options.store.state.login.identity;
      //未登录
      if (fullPathRoot === 'home') {
        next();
        return;
      }
      if (auth == 2) {
        next({ name: 'login' });
        return;
      }
      //已登录的情况再去登录页，跳转至首页
      if (fullPathRoot === 'login') {
        if (auth === 1) {
          next({ name: 'home' });
          return;
        }
      }
      if (fullPathRoot === 'teacher_center') {
        if (identity == 'T') {
          next();
          return;
        } else {
          next({ name: '404' });
          return;
        }
      }
      if (fullPathRoot === 'student_center') {
        if (identity == 'S') {
          next();
          return;
        } else {
          next({ name: '404' });
          return;
        }
      }
    });
  } else {
    next();
    return;
  }
  next();
  return;
});

router.afterEach(() => {
  {
    NProgress.done();
  }
});
export default router;
